package com.longlong.utils;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 七牛云上传工具类
 */

@Component
@Slf4j
public class QiniuUtil {


    @Value("${qiniu.accessKey}")
    private String accessKey;


    @Value("${qiniu.secretKey}")
    private String secretKey;


    @Value("${qiniu.bucket}")
    private String bucket;


    /**
     * 根据文件路径去上传文件
     * @param filepath
     * @param filename
     * @return
     */
    public String upload(String filepath, String filename){
        //构造配置
        Configuration cfg = new Configuration(Region.region2());
        UploadManager uploadManager = new UploadManager(cfg);
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        filename = this.genName(filename);
        try{
            Response put = uploadManager.put(filepath, filename, upToken);
            return filename;
        }catch (QiniuException e){
            Response r = e.response;
            try{
                log.error("文件上传失败=============>{}",r.bodyString());
            }catch (QiniuException ex2){

            }
            return null;
        }
    }


    /**
     * 根据字节长传稳健
     * @param bytes
     * @param filename
     * @return
     */
    public String upload(byte[] bytes, String filename){
        //构造配置
        Configuration cfg = new Configuration(Region.region2());
        UploadManager uploadManager = new UploadManager(cfg);
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        filename = this.genName(filename);
        try{
            Response put = uploadManager.put(bytes, filename, upToken);
            return filename;
        }catch (QiniuException e){
            Response r = e.response;
            try{
                log.error("文件上传失败=============>{}",r.bodyString());
            }catch (QiniuException ex2){

            }
            return null;
        }
    }



    /**
     * 根据流长传稳健
     * @param stream
     * @param filename
     * @return
     */
    public String upload(InputStream stream, String filename){
        //构造配置
        Configuration cfg = new Configuration(Region.region2());
        UploadManager uploadManager = new UploadManager(cfg);
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        filename = this.genName(filename);
        try{
            Response response = uploadManager.put(stream, filename, upToken,null,null);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            log.info("文件上传成功=====>{}",putRet);
            return filename;
        }catch (QiniuException e){
            Response r = e.response;
            try{
                log.error("文件上传失败=============>{}",r.bodyString());
            }catch (QiniuException ex2){

            }
            return null;
        }
    }


    /**
     * 根据文件名删除文件
     * @param fileName
     */
    public void delete(String fileName){
        //构造一个指定的Region对象
        Configuration cfg = new Configuration(Region.region2());
        Auth auth = Auth.create(accessKey, secretKey);
        BucketManager bucketManager = new BucketManager(auth, cfg);
        try {
            bucketManager.delete(bucket,fileName);
            log.info("删除文件成功");
        }catch (QiniuException ex){
            log.error("删除文件失败==============>code{}",ex.code());
            log.error(ex.response.toString());
        }
    }


    /**
     * 根据时间生成文件名
     * @return
     */
    public String genName(String fileName){
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        return format.format(new Date()) + fileName;
    }
}















